package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.ItemUser;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDailyService dailyService;

    @Autowired
    private IUserCreditService creditService;

    @Autowired
    private IItemUserService itemUserService;

    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping
    public JSONResult register(User user) {
        JSONResult result = new JSONResult();
        try {
            userService.register(user);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping
    public JSONResult updateInfo(User user) {
        JSONResult result = new JSONResult();
        try {
            userService.updateInfo(user);
            result.setData(userService.get(user.getId()));
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/{authorId}/travels")
    public Object updateInfo(TravelQueryObject qo) {
        return travelService.query(qo);
    }

    @GetMapping("/{authorId}/dailys")
    public Object getDailys(DailyQueryObject qo) {
        return dailyService.query(qo);
    }

    @GetMapping("/{userId}/credits")
    public Object getCreditsByUserId(@PathVariable Long userId) {
        return creditService.getCreditsByUserId(userId);
    }
    /**
     * 查询一个用户的信息
     *
     * @return
     */
    @GetMapping("/{userId}")
    public Object getAUserInfo(@PathVariable Long userId) {
        //查询用户信息
        return userService.get(userId);
    }


    /**
     * 购物车记录
     * @param itemUser
     * @return
     */
    @PostMapping("/items")
    public JSONResult updateInfo(ItemUser itemUser) {
        JSONResult result = new JSONResult();
        try {
            itemUserService.insert(itemUser);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
